package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsArticleMapper;
import com.shop.pojo.DtsArticle;
import com.shop.pojo.DtsBrand;
import com.shop.service.DtsArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DtsArticleServiceImpl
 * @Author xiao宋
 * @Date 11:50  2022/10/22
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsArticleServiceImpl implements DtsArticleService {
    @Autowired
    DtsArticleMapper articleMapper;
    @Override
    public Map<String, Object> selectPage(Integer page, Integer limit, String sort, String order, String title) {
        IPage<DtsArticle> iPage = new Page<>(page, limit);
        QueryWrapper<DtsArticle> w = new QueryWrapper<>();
        w.eq("deleted", false);
//        公告标题 name
        if (!StringUtils.isEmpty(title)) {
            w.eq("title", title);
        }
        //        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }
        articleMapper.selectPage(iPage, w);
        Map<String, Object> result = new HashMap<>();
        result.put("total", iPage.getTotal());
        result.put("items", iPage.getRecords());
        return result;
    }

    @Override
    public void creat(DtsArticle article) {
        article.setAddTime(new Date());
        article.setUpdateTime(new Date());
        articleMapper.insert(article);
    }

    @Override
    public void delete(DtsArticle article) {
        article.setDeleted(true);
        article.setUpdateTime(new Date());
        articleMapper.updateById(article);
    }

    @Override
    public void update(DtsArticle article) {
        article.setUpdateTime(new Date());
        articleMapper.updateById(article);
    }
}
